import os
import threading

from apscheduler.schedulers.blocking import BlockingScheduler

from DataClean.ErroPdfClean import ErroPdfClean
from DataClean.NoSqlPdfClean import NoSqlPdfClean
from DataClean.NoPdfSqlClean import NoPdfSqlClean
from DataDeduplication.SqldataDeduplication import SqldataDuplication
from DataDelete.JgSqldelete import Jgsqldelete
from DataExport.CjFileToJg import CjFileToJg
from DataExport.CjSqlToJg import CjSqlToJg


def errpdfclean():  # 错误pdf数据清理
    ErroPdfClean().epdfclean()

def cjfiletojg():   # 采集pdf文件上传到加工服务器
    CjFileToJg().export_law()

def start_job1():
    # 无sql记录的pdf文件清理
    result1 = NoSqlPdfClean().pdfclean()
    while result1 != True:
        result1 = NoSqlPdfClean().pdfclean()

def start_job2():
    # 无pdf文件的sql记录清理
    result1 = NoPdfSqlClean().sqlclean()
    while result1 != True:
        result1 = NoPdfSqlClean().sqlclean()
    else:
        # sql重复数据清理
        result2 = SqldataDuplication().sqldataduple()
        while result2 != True:
            result2 = SqldataDuplication().sqldataduple()
        else:
            # 加工数据库删除sql记录
            result3 = Jgsqldelete().sqldelete()
            while result3 != True:
                result3 = Jgsqldelete().sqldelete()
            else:
                # 采集的sql记录上传到加工服务器
                result4 = CjSqlToJg().exportsql()


def start():
    print("数据治理开始")
    thrad_list1 = []
    thrad_list2 = []
    thread1 = threading.Thread(target=start_job1)
    thread2 = threading.Thread(target=errpdfclean)
    thrad_list1.append(thread1)
    thrad_list1.append(thread2)
    for t in thrad_list1:
        t.setDaemon(True)
        t.start()
    for t in thrad_list1:
        t.join()
    thread3 = threading.Thread(target=cjfiletojg)
    thread4 = threading.Thread(target=start_job2)
    thrad_list2.append(thread3)
    thrad_list2.append(thread4)
    for t in thrad_list2:
        t.setDaemon(True)
        t.start()
    for t in thrad_list2:
        t.join()
    print("数据治理结束")


def main():
    scheduler = BlockingScheduler()
    scheduler.add_job(start, 'cron',day_of_week = '6', hour='2')
    print("数据治理服务将在每周日早上2点启动")
    scheduler.start()


if __name__ == '__main__':
    # start()
    main()
